package no.rodland.acc.model;

import org.joda.time.DateTime;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import no.rodland.acc.db.AbstractRow;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jun 18, 2010 12:04:44 PM
 * @Author: fmr
 */
public class TextBudgetAccount extends AbstractRow {
    private String text;
    private BudgetAccount budgetAccount;

    public TextBudgetAccount(int id, String text, BudgetAccount budgetAccount, DateTime updated, DateTime created) {
        super(id, updated, created);
        this.budgetAccount = budgetAccount;
        this.text = text;
    }

    protected TextBudgetAccount(ResultSet rs, Map<Integer, BudgetAccount> budgetAccounts) throws SQLException {
        super(rs.getInt("id"),
                new DateTime(rs.getTimestamp("updated_date")),
                new DateTime(rs.getTimestamp("created_date")));
        budgetAccount = budgetAccounts.get(rs.getInt("budget_account_id"));
    }

    @Override
    public String getTableName() {
        return "budget_account_groups";
    }

    @Override
    public Object[] getData() {
        return new Object[]{text, budgetAccount.getId()};
    }

    @Override
    public String[] getColumns() {
        return new String[]{"budget_account_id", "account_group_id"};
    }

    public BudgetAccount getBudgetAccount() {
        return budgetAccount;
    }

    public String getText() {
        return text;
    }

    @Override
    public boolean usesIdentity() {
        return true;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("TextBudgetAccount");
        sb.append("{text='").append(text).append('\'');
        sb.append(", budgetAccount=").append(budgetAccount);
        sb.append('}');
        return sb.toString();
    }

    public static ParameterizedRowMapper<TextBudgetAccount> getRowMapper(final Map<Integer, BudgetAccount> budgetAccounts) {
        return new ParameterizedRowMapper<TextBudgetAccount>() {
            @Override
            public TextBudgetAccount mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new TextBudgetAccount(rs.getInt("id"),
                        rs.getString("text"),
                        budgetAccounts.get(rs.getInt("budget_account_id")),
                        new DateTime(rs.getTimestamp("updated_date")),
                        new DateTime(rs.getTimestamp("created_date")));
            }
        };
    }

    public static BudgetAccount getBudgetAccountForText(String text,
                                                  Map<Integer, TextBudgetAccount> textBudgetAccounts) {
        if (text == null || textBudgetAccounts == null) {
            return null;
        }
        for (TextBudgetAccount textBudgetAccount : textBudgetAccounts.values()) {
            if (textBudgetAccount.getText() == null) {
                return null;
            }
            String key = textBudgetAccount.getText().toLowerCase();
            String lower = text.toLowerCase();
            if (lower.indexOf(key) > -1) {
                return textBudgetAccount.getBudgetAccount();
            }
        }
        return null;
    }

    @SuppressWarnings({"RedundantIfStatement"})
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TextBudgetAccount that = (TextBudgetAccount) o;

        if (getId() != that.getId()) {
            return false;
        }
        if (budgetAccount != null ? !budgetAccount.equals(that.budgetAccount) : that.budgetAccount != null) {
            return false;
        }
        if (text != null ? !text.equals(that.text) : that.text != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = text != null ? text.hashCode() : 0;
        result = 31 * result + (budgetAccount != null ? budgetAccount.hashCode() : 0);
        result = 31 * result + getId();
        return result;
    }
}